﻿//Newton's cannon on mountain.

package
{
	import flash.display.*;
	import flash.text.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	[SWF(width="615", height="643")]
		
	
	public class NewtMtn extends Sprite {
		private var firing:Sound;
		private var LeavingEarth:Sound;
		private var Crash:Sound;
		private var Comeback:Sound;
		private var bitimage:Loader = new Loader();
		
		
		public var controlpanel:ControlPanel;
						
		private var FiringSpeed_TextField: TextField;
		private var format1:TextFormat;
		
		private var ball_1:Ball;
		
		private var speedSlider:HorizontalSlider;
		private var go:Boolean = true;
		private var fiered:Boolean = false;
		
		private var angle: Number = angle_0;
		private var center_1Y:Number = 327;
		private var center_1X:Number = 308;
		private var t:Number = 0;
		private var delta_t: Number = 0.5;
		
		private var r_0: Number = 218;
		private var angle_0: Number = 0.08;
		private var r_E: Number = 185;
		private var r: Number = r_0;
		private var v_0: Number = 1;
		private var GM: Number = 11000;
		private var delta_angle: Number = 0;
		private var panely: Number = 608 
				
		//private var bitimage:Bitmap = new Bitmap();
				
		public function NewtMtn()	{
			init();
		}
		public function init():void {
			
			firing = new Sound(new URLRequest("firing.mp3"));
			LeavingEarth = new Sound(new URLRequest("LeavingEarth.mp3"));
			Crash = new Sound(new URLRequest("Crash.mp3"));
			Comeback = new Sound(new URLRequest("Comeback.mp3"));
			bitimage = new Loader;
			bitimage.load(new URLRequest("NewtMtn_3.JPG"));
			addChild(bitimage);
			//var style:StyleSheet = new StyleSheet();
			
			//bmp = new background();		
			//addChild(bmp);
			
			//var pictLdr:Loader = new Loader();
			//var pictURL:String = "NewtMtn_3.jpg"
			//var pictURLReq:URLRequest = new URLRequest(pictURL);
			//pictLdr.load(pictURLReq);
			//this.addChild(pictLdr);
			//pictLdr.scaleX = 1;
			//pictLdr.scaleY = 1;
			//pictLdr.x = 0;
			//pictLdr.y = 0;
			
			//bitimage.bitmapData = img;
			//var bitimage:Bitmap = new newtmtn();

			
			
			controlpanel = new ControlPanel();
			addChild(controlpanel);
			controlpanel.y = panely;
			controlpanel.slider1 = 4.44;
			controlpanel.draw();
			
			
			
			format1 = new TextFormat();
            format1.color = 0xFF0000;
			format1.font = "Courier";
			format1.size = 12;
												
			FiringSpeed_TextField= new TextField();
			addChild(FiringSpeed_TextField);
			FiringSpeed_TextField.text = "Firing Speed \n km/sec";
			FiringSpeed_TextField.x = 470;
			FiringSpeed_TextField.y = panely;
			
			ball_1 = new Ball(5, 0xff0000, 0x000000);
			addChild(ball_1);
			ball_1.x = center_1X + Math.sin(angle_0) * r_0;
			ball_1.y = center_1Y - Math.cos(angle_0) * r_0;
						
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function onEnterFrame(event:Event):void {
			//controlpanel.y = 608;
			//speedSlider.y = 615;
			ball_1.y = center_1Y - Math.cos(angle) * r;
			ball_1.x = center_1X + Math.sin(angle) * r;
							
			if(go) {
				if(!fiered) {v_0 = controlpanel.slider1;}
				if(!controlpanel.pause) {
					t = delta_t;
					NewCannonBallPosition();
					if(!fiered){firing.play();}
					fiered = true;
				}
				if(controlpanel.stepback) {
					t = (-1)*delta_t;
					NewCannonBallReversePosition()
				}
				if(controlpanel.stepforward) {
					t = delta_t;
					NewCannonBallPosition();
				}
				if(controlpanel.reset) {
					angle = angle_0;
					r = r_0;
					v_0 = controlpanel.slider1;
					controlpanel.pause = true;
					fiered = false;
				}
			}
			else {
				controlpanel.pause = true;
				v_0 = controlpanel.slider1;
				if(controlpanel.reset) {
					angle = angle_0;
					r = r_0;
					go = true;
					fiered = false;
				}
			}
			
			
		}
	
		public function NewCannonBallPosition():void {
	
			r = r_0/((Math.cos(angle) + (1 - Math.cos(angle))*GM/(r_0*v_0*v_0)));
			delta_angle = (r_0*v_0)*t/(r*r);
			angle += delta_angle;
			
			if(r<r_E){
				controlpanel.pause = true;
				Crash.play();
				go = false;
			}
			if(angle>(angle_0 + 1.99*Math.PI)){
				controlpanel.pause = true;
				Comeback.play();
				go = false;
			}
			if(ball_1.x > stage.stageWidth && v_0 > 10.36){
				controlpanel.pause = true;
				LeavingEarth.play();
				go = false;
			}
	}
			
		public function NewCannonBallReversePosition():void{
	
			delta_angle = (r_0*v_0)*t/(r*r);
			angle += delta_angle;
			r = r_0/((Math.cos(angle) + (1 - Math.cos(angle))*GM/(r_0*v_0*v_0)));
			if (angle<angle_0){
				controlpanel.pause = true;
				angle = angle_0;
				r = r_0;
				go = false;
			}
				
			if(r<r_E){
				controlpanel.pause = true;
				Crash.play();
				go = false;
			}
			if(angle>(angle_0 + 1.99*Math.PI)){
				controlpanel.pause = true;
				Comeback.play();
				go = false;
			}
			if(ball_1.x > stage.stageWidth && v_0 > 10.36){
				controlpanel.pause = true;
				LeavingEarth.play();
				go = false;
			}
	}
					
	}
}
	
	
